/**
 * $Revision: 1.0 $
 * $Author: Eric Yang $
 * $Date: Nov 22, 2009 11:31:52 PM $
 *
 * Author: Eric Yang
 * Date  : Nov 22, 2009 11:31:52 PM
 *
 */
package com.gooex.web.action.gbuy.product;

import java.util.ArrayList;
import java.util.List;
import org.c4j.string.StringTools;

import com.gooex.domain.base.Product;
import com.gooex.domain.base.Property;
import com.gooex.service.base.IProductManager;
import com.gooex.web.action.BaseProtectedAction;


/**
 * @author Eric Yang
 * @version 1.0
 */
public class PropertyValueAction extends BaseProtectedAction {

	private IProductManager productManager;

	private Product product;

	private Property property;

	private List<String> values;

	private String productId;

	private String propertyId;

	public String edit() throws Exception {
		property = propertyManager.getProperty(propertyId);
		initProduct();
		return SUCCESS;
	}

	public String save() throws Exception {
		if (values == null) {
			values = new ArrayList<String>();
		}
		for (int i = values.size() - 1; i >= 0; i--) {
			if (StringTools.isBlankOrNull(values.get(i))) {
				values.remove(i);
			}
		}
		if (values.size() == 0) {
			addActionError(getText("product.property.values.null"));
			initProduct();
			return INPUT;
		}
		property = propertyManager.saveOrUpdateProperty(property, values);
		initProduct();
		return SUCCESS;
	}

	public String remove() throws Exception {
		property = propertyManager.getProperty(propertyId);
		propertyManager.removeProperty(property);
		return SUCCESS;
	}

	private void initProduct() {
		if (productId != null)
			product = productManager.getProduct(productId);

		if (property != null && product == null)
			product = property.getProduct();

		productId = product.getId();
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public String getPropertyId() {
		return propertyId;
	}

	public void setPropertyId(String propertyId) {
		this.propertyId = propertyId;
	}

	public Property getProperty() {
		return property;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductManager(IProductManager productManager) {
		this.productManager = productManager;
	}

	public List<String> getValues() {
		return values;
	}

	public void setValues(List<String> values) {
		this.values = values;
	}

	public void setProperty(Property property) {
		this.property = property;
	}
}
